/*
 * Created on 30.01.2004
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package ro.qi.framework.sys.entities;

import java.sql.Clob;
import java.util.Date;
import java.util.Hashtable;
import java.util.Vector;

import ro.qi.framework.sys.debug.Logger;
import ro.qi.framework.sys.entities.components.EntityComponent;
import ro.qi.framework.sys.entities.exception.ComponentException;
import ro.qi.framework.sys.repository.Repository;

/**
 * @author Vlad Lepadatu
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class Entity {

	protected Hashtable components = new Hashtable();
	protected boolean lastActionError = false;
	protected String lastActionErrorDescription = "";

	public Entity() {
		Repository.getEntity(this);
	}

	protected boolean getBooleanValue(String componentName) {
		try {
			Object value = getValue(componentName);
			if( value != null )
				return (((Integer)value).intValue()==1)?true:false;
		} catch (NumberFormatException e) {}
		return false;
	}

	protected int getIntValue(String componentName) {
		try {
			Object value = getValue(componentName);
			if( value != null )
				return ((Integer)value).intValue();
		} catch (NumberFormatException e) {}
		return 0;
	}

	protected long getLongValue(String componentName) {
		try {
			Object value = getValue(componentName);
			if( value != null )
				return ((Long)value).longValue();
		} catch (NumberFormatException e) {}
		return 0;
	}

	protected float getFloatValue(String componentName) {
		try {
			Object value = getValue(componentName);
			if (value != null)
				return ((Float)value).floatValue();
		} catch (NumberFormatException e) {}
		return 0;
	}

	protected double getDoubleValue(String componentName) {
		try {
			Object value = getValue(componentName);
			if( value != null )
				return ((Double)value).doubleValue();
		} catch (NumberFormatException e) {}
		return 0;
	}

	protected String getStringValue(String componentName) {
		Object value = getValue(componentName);
		if (value == null)
			value = "";
		return String.valueOf(value);
	}

	protected Date getDateValue(String componentName) {
		return (Date)getValue(componentName);
	}

	public Object getValue(String componentName) {
		if( components.containsKey(componentName) ) {
			EntityComponent component = (EntityComponent)components.get(componentName);
			if( component != null )
				return component.getValue();
		} else {
			Logger.error(this, "Component [" + componentName + "] not found, could not retreive it's value");
		}
		return null;
	}

	protected void setValue(String componentName, int componentValue) {
		setValue(componentName, new Integer(componentValue));
	}

	protected void setValue(String componentName, float componentValue) {
		setValue(componentName, new Float(componentValue));
	}

	protected void setValue(String componentName, double componentValue) {
		setValue(componentName, new Double(componentValue));
	}

	protected void setValue(String componentName, boolean componentValue) {
		setValue(componentName, new Integer((componentValue)?1:0));
	}

	public void setValue(String componentName, Object componentValue) {
		if( components.containsKey(componentName) ) {
			EntityComponent component = (EntityComponent)components.get(componentName);
			if( component != null ) {
				try {
					component.setValue(componentValue);
				} catch (ComponentException e) {
					Logger.error(this, "Error settings component [" + componentName + "] value: " + e.getMessage());
				}
			}
		} else {
			Logger.error(this, "Component [" + componentName + "] not found, could not set the value " + componentValue.toString());
		}
	}

	protected int getCollectionSize(String collectionName) {
		if (getValue(collectionName) == null) {
			return 0;
		}
		return ((Vector)getValue(collectionName)).size();
	}

	protected Object getCollectionValue(String collectionName, String valueName, int index) {
		if( index < getCollectionSize(collectionName) ) {
			Hashtable tmp = (Hashtable)(((Vector<?>)getValue(collectionName)).get(index));
			if( tmp.containsKey(valueName) )
				return tmp.get(valueName);
		}
		return null;
	}

	protected String getCollectionStringValue(String collectionName, String valueName, int index) {
		Object value = getCollectionValue(collectionName, valueName, index);
		if (value != null)
			return String.valueOf(value);
		return "";
	}

	protected String getCollectionClobValue(String collectionName, String valueName, int index) {
		Object value = getCollectionValue(collectionName, valueName, index);
		if (value == null) {
			return "";
		} else
		if (value instanceof Clob) {
			Clob clob = (Clob)value;
			if( clob != null ) {
				try {
					return clob.getSubString(1,(int)(clob.length()+1));
				} catch (Exception e) {
				}
			}
			return "";
		} else {
			return String.valueOf(value);
		}
	}

	protected int getCollectionIntValue(String collectionName, String valueName, int index) {
		Object value = getCollectionValue(collectionName, valueName, index);
		if (value != null) {
			try {
				return Integer.parseInt(value.toString());
			} catch (NumberFormatException n) {}
		}
		return 0;
	}

	protected Date getCollectionDateValue(String collectionName, String valueName, int index) {
		Object value = getCollectionValue(collectionName, valueName, index);
		if (value != null) {
			return (Date)value;
		}
		return null;
	}

	protected float getCollectionFloatValue(String collectionName, String valueName, int index) {
		Object value = getCollectionValue(collectionName, valueName, index);
		if (value != null) {
			try {
				return Float.parseFloat(value.toString());
			} catch (NumberFormatException n) {}
		}
		return 0;
	}

	protected boolean getCollectionBooleanValue(String collectionName, String valueName, int index) {
		return getCollectionIntValue(collectionName, valueName, index) != 0;
	}

	public void select() {
		Repository.select(this);
	}

	public void update() {
		Repository.update(this);
	}

	public void add() {
		Repository.add(this);
	}

	public void delete() {
		Repository.delete(this);
	}

	public void action(String action) {
		Repository.action(this, action);
	}

	public void setComponents(Hashtable<?, ?> hashtable) {
		components = hashtable;
	}

	/**
	 * @return
	 */
	public void setLocaleId(int value) {
		setValue("localeId", value);
	}

	public int getLocaleId() {
		return getIntValue("localeId");
	}

	/**
	 * @return
	 */
	public boolean isLastActionError() {
		return lastActionError;
	}

	/**
	 * @return
	 */
	public String getLastActionErrorDescription() {
		return lastActionErrorDescription;
	}

	/**
	 * @param b
	 */
	public void setLastActionError(boolean b) {
		lastActionError = b;
	}

	/**
	 * @param string
	 */
	public void setLastActionErrorDescription(String string) {
		lastActionErrorDescription = string;
	}

}
